<!--学生上传毕设资料-->
<style>
	.jindu {
		margin-bottom: 100px;
	}
	
	.jindu p {
		float: left;
		text-align: center;
		width: 25%;
	}
	
	.bottom_10 {
		margin-bottom: 20px;
	}
	
	#tishi {
		color: red;
		font-size: 18px;
		text-decoration: none;
		line-height: 2;
		/*display: none;*/
	}
	
	#div_context {
		height: 460px;
		word-wrap: break-word;
		word-break: normal;
		border: 1px solid #3C3C3C;
		margin: 10px 10px;
		overflow: auto;
	}
	
	.modal {
		position: fixed;
	}
</style>

<div class="modal-dialog modal fade" id="word_context">
	<div class="modal-content">
		<div class="modal-header">
			<h4>资料预览(无格式)</h4>
			<button type="button" class="close" data-dismiss="modal">&times;</button>
		</div>
		<div class="modal-body" id="div_context">
			<div id="div_context">
				--
			</div>
		</div>
		<div class="modal-footer">
			<button id="ture" type="button" class="btn btn-primary" class="close" data-dismiss="modal">确定</button>
		</div>
	</div>
</div>

<h3 class="bottom_10">上传毕设资料</h3>
<div class="progress">
	<div id="jieduan1" style="width: 25%" class="progress-bar-success progress-bar-striped active">
		<span class="sr-only">25% Complete (success)</span>
	</div>
	<div id="jieduan2" style="width: 25%" class="progress-bar-success progress-bar-striped active">
		<span class="sr-only">25% Complete (success)</span>
	</div>
	<div id="jieduan3" style="width: 25%" class="progress-bar-success progress-bar-striped active">
		<span class="sr-only">25% Complete (success)</span>
	</div>
	<div id="jieduan4" style="width: 25%" class="progress-bar-success progress-bar-striped active">
		<span class="sr-only">25% Complete (success)</span>
	</div>
</div>
<div class="jindu">
	<div>
		<p>开题报告阶段</p>
		<p>任务书阶段</p>
		<p>初稿阶段</p>
		<p>定稿阶段</p>
	</div>
	<div id="jindu_p">
		<p>--</p>
		<p>--</p>
		<p>--</p>
		<p>--</p>
	</div>
</div>
<div class="input-group bottom_10">
	<input id='location' class="form-control" onclick="$('#i-file').click();" placeholder="可重复提交">
	<label class="input-group-btn">
  		<button id="i-check" class="btn btn-primary" onclick="$('#i-file').click();">选择文件</button>
   </label>
	<form action="/" method="post" id="form">
		<input type="file" name="fileName" id='i-file' accept=".doc, .docx" onchange="$('#location').val($('#i-file').val());" style="display: none">
		<input type="hidden" name="type" id="type_stage" value="" />
	</form>
</div>
<div class="tijiao">
	<span id="tishi" style="display: none;">您在该阶段还未提交资料，请提交！</span>
	<a id="sel_word" style="display: none;line-height: 2;">您已提交过资料，点击查看</a>
	<button id="tijiao" class="btn btn-primary" style="float: right;">提交资料</button>
</div>

<script>
	var myajax = $.ajax({
		xhrFields: {
			withCredentials: true
		},
		type: 'GET',
		url: getFullURL('user/get-stage'),
		data: {},
		success: function(data) {
			if(data.status == 0) {
				console.log(data)
				$.each(data.list, function(i, n) {
					var index = $('#jindu_p p').eq(i);
					var time = "开始：" + n.start + "<br/>" + "结束：" + n.end;
					index.html(time);

					if(data.stage == i + 1) {
						$.session.set("start_", n.start);
						$.session.set("end_", n.end);
					}
				})

				$.session.set("stage", data.stage);
			}
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})

	//	progress-bar
	$.when(myajax).done(function() {
		if($.session.get("stage") == 1) {
			$('#jieduan1').addClass("progress-bar");
		} else if($.session.get("stage") == 2) {
			$('#jieduan1').addClass("progress-bar");
			$('#jieduan2').addClass("progress-bar");
		} else if($.session.get("stage") == 3) {
			$('#jieduan1').addClass("progress-bar");
			$('#jieduan2').addClass("progress-bar");
			$('#jieduan3').addClass("progress-bar");
		} else if($.session.get("stage") == 4) {
			$('#jieduan1').addClass("progress-bar");
			$('#jieduan2').addClass("progress-bar");
			$('#jieduan3').addClass("progress-bar");
			$('#jieduan4').addClass("progress-bar");
		}

		$('#type_stage').val($.session.get("stage") - 1);

		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'GET',
			url: getFullURL('user/is-put-file'),
			data: {
				stage: $.session.get("stage")
			},
			success: function(data) {
				if(data.status == 0) {
					console.log(data)
					if(data.flag == true) {
						$('#tishi').css("display", "none")
						$('#sel_word').css("display", "inline-block")
						sel_words()
					} else {
						$('#tishi').css("display", "inline-block")
						$('#sel_word').css("display", "none")
					}
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	function sel_words() {
		$('#sel_word').click(function() {
			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				type: 'GET',
				url: getFullURL('sel-word'),
				data: {
					type: $.session.get("stage") - 1
				},
				success: function(data) {
					if(data.status == 0) {
						console.log(data)
						$('#div_context').html(data.value)
						$(word_context).modal({
							keyboard: true,
							show: true
						})
					}
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
	}

	$('#tijiao').click(function() {

		var start_date = new Date($.session.get("start_")).getTime();
		var end_date = new Date($.session.get("end_")).getTime();
		var myDate = new Date().getTime();

		if(myDate < start_date || myDate > end_date) {
			alert("未在活动时间！");
			return;
		}

		if($('#i-file').val() == null || $('#i-file').val() == "") {
			alert("还未选择资料！");
			return;
		}

		var formData = new FormData($("#form")[0]);

		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'POST',
			url: getFullURL('user/up-bishefile'),
			data: formData,
			dataType: 'json',
			cache: false,
			processData: false, // 不加会报错
			contentType: false, // 不加会报错
			success: function(data) {
				if(data.status == 0) {
					console.log(data)
					alert("上传成功！")
					setTimeout(function() {
						$('.center').html('');
						$('.center').load('./completionphase.html');
					}, 300);
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	function timeStamp2String(time) {
		var datetime = new Date();
		datetime.setTime(time);
		var year = datetime.getFullYear();
		var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	}
</script>